//
//  LGShowControllerTableVC.m
//  ALS3.0
//
//  Created by guomianxiang on 17/3/17.
//  Copyright © 2017年 guomianxiang. All rights reserved.
//

#import "LGShowControllerTableVC.h"
#import "LGControllerInfoViewController.h"
#import "LGDCTableHead.h"
#import "ALSHeader.h"
@interface LGShowControllerTableVC ()<UIViewControllerPreviewingDelegate>

@property (strong, nonatomic) NSMutableArray <NSMutableArray <LGDCCellContent *>*>*tableDataArr;

@property (strong, nonatomic) NSArray <NSArray <LGController *>*>*controllerArr;

@property (assign, nonatomic) BOOL reload;

@end


@implementation LGShowControllerTableVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNotification];
    [MobClick beginLogPageView:UMPageName];
    self.reload = YES;
    
    self.controllerArr = [NSArray arrayWithObjects:DCMI.controllers, nil];
    self.tableDataArr = [LGDCCellContent transfromCellContentArrWithControllersArr:self.controllerArr];
    k_registerNib(self.tableView);
    
    [self registerForPreviewingWithDelegate:self sourceView:self.tableView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (_reload) {
        [self.tableView reloadData];
        _reload = NO;
    }
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:UMPageName];
}


- (void)setNotification {
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(refreshData) name:CMAddedItemNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(refreshData) name:CMItemChangedNotification object:nil];
}

- (void)refreshData {
    self.controllerArr = [NSArray arrayWithObjects:DCMI.controllers, nil];
    self.tableDataArr = [LGDCCellContent transfromCellContentArrWithControllersArr:self.controllerArr];
    
    self.reload = YES;
}

#pragma mark - UIViewControllerPreviewingDelegate



- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location {
    LGDCBaseCell *cell = [self searchCellWithPoint:location];

    if (cell.content.controller == nil) {
        return nil;
    }
    
    previewingContext.sourceRect = cell.frame;
    
    UIStoryboard *DCSB = [UIStoryboard storyboardWithName:@"DoorController" bundle:nil];
    
    LGControllerInfoViewController *controllerInfoVC = [DCSB instantiateViewControllerWithIdentifier:NSStringFromClass([LGControllerInfoViewController class])];

    controllerInfoVC.editControllerItem = cell.content.controller;
    
    return controllerInfoVC;
}

- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
    [self.navigationController pushViewController:viewControllerToCommit animated:YES];
}

- (LGDCBaseCell *)searchCellWithPoint:(CGPoint)point {
    
    LGDCBaseCell *cell = nil;
    for (UIView *view in self.tableView.subviews) {
        NSString *class = [NSString stringWithFormat:@"%@",view.class];
        if (![class isEqualToString:@"UITableViewWrapperView"]) continue;
        for (UIView *tempView in view.subviews) {
            if ([tempView isKindOfClass:[LGDCBaseCell class]] && CGRectContainsPoint(tempView.frame, point)) {
                cell = (LGDCBaseCell *)tempView;
                break;
            }
        }
        break;
    }
    return cell;
}


#pragma mark - Table view data source

k_tableViewDataSourceAndCellHeight(tableDataArr, nil);

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [self performSegueWithIdentifier:@"showControllerTVCToInfoTableVC" sender:self.tableDataArr[indexPath.section][indexPath.row].controller];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        __weak typeof (self) weakSelf = self;
        [DCMI removeItem:self.controllerArr[indexPath.section][indexPath.row] reply:^(BOOL success, NSError *error) {
            if (success) {
                
                [weakSelf refreshData];
                
                [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            }else {
                
            }
        }];
        
    }
}


#pragma mark - Navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    LGControllerInfoViewController *infoVC = segue.destinationViewController;
    infoVC.editControllerItem = sender;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    
}

@end
